package com.ruoyi.project.system.borrow.controller;

import java.util.List;

import com.ruoyi.project.system.approval.domain.Approval;
import com.ruoyi.project.system.approval.service.IApprovalService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.item.domain.Item;
import com.ruoyi.project.system.item.service.IItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.borrow.domain.Borrow;
import com.ruoyi.project.system.borrow.service.IBorrowService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 物品借阅Controller
 *
 * @author stefan
 * @date 2021-11-10
 */
@Controller
@RequestMapping("/system/borrow")
public class BorrowController extends BaseController {
    private String prefix = "system/borrow";

    @Autowired
    private IBorrowService borrowService;

    @Autowired
    private IItemService itemService;
    @Autowired
    private IApprovalService approvalService;

    @RequiresPermissions("system:borrow:view")
    @GetMapping()
    public String borrow() {
        return prefix + "/borrow";
    }

    /**
     * 查询物品借阅列表
     */
    @RequiresPermissions("system:borrow:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Borrow borrow) {
        startPage();
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        return getDataTable(list);
    }


    /**
     * 导出物品借阅列表
     */
    @RequiresPermissions("system:borrow:export")
    @Log(title = "物品借阅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Borrow borrow) {
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        ExcelUtil<Borrow> util = new ExcelUtil<Borrow>(Borrow.class);
        return util.exportExcel(list, "物品借阅数据");
    }

    /**
     * 新增物品借阅
     */
    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("items", itemService.selectItemAll());
        List<Approval> approvals = approvalService.selectApprovalListAll();
        model.addAttribute("approvalList",approvalService.selectApprovalListAll());
        return prefix + "/add";
    }

    /**
     * 新增保存物品借阅
     */
    @RequiresPermissions("system:borrow:add")
    @Log(title = "物品借阅", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Borrow borrow) {
        return toAjax(borrowService.insertBorrow(borrow));
    }

    /**
     * 修改物品借阅
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Borrow borrow = borrowService.selectBorrowById(id);
        mmap.put("borrow", borrow);
        return prefix + "/edit";
    }

    /**
     * 修改保存物品借阅
     */
    @RequiresPermissions("system:borrow:edit")
    @Log(title = "物品借阅", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Borrow borrow) {
        return toAjax(borrowService.updateBorrow(borrow));
    }

    /**
     * 删除物品借阅
     */
    @RequiresPermissions("system:borrow:remove")
    @Log(title = "物品借阅", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(borrowService.deleteBorrowByIds(ids));
    }
}
